#pragma once
 
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <pthread.h>
 
struct ThreadInfo
{
    pthread_t tid;
    std::string name;
};
 
static const int defalutnum = 5;
 
template<class T>
class ThreadPool
{
private:
    void Lock()
    {
        pthread_mutex_lock(&mutex_);
    }
 
    void Unlock()
    {
        pthread_mutex_unlock (&mutex_);
    }
 
    void Wakeup()
    {
        pthread_cond_signal(&cond_);
    }
 
    void ThreadSleep()
    {
        pthread_cond_wait(&cond_, &mutex_);
    }
 
    bool IsQueueEmpty()
    {
        return tasks_.size() == 0 ? true : false;
    }
 
    std::string GetThreadName(pthread_t tid)
    {
        for (const auto &ti : threads_)
        {
            if (ti.tid == tid)
                return ti.name;
        }
        return "None";
    }
public:
    static void *HandlerTask(void *args)
    {
        ThreadPool<T> *tp = static_cast<ThreadPool<T> *>(args);
 
        while (true)
        {
            tp->Lock();
 
            while (tp->IsQueueEmpty())
            {
                tp->ThreadSleep();
            }
            T t = tp->Pop();
 
            tp->Unlock();
 
            t();
        }
    }
 
    void Start()
    {
        int num = threads_.size();
        for (int i = 0; i < num; i++)
        {
            threads_[i].name = "thread-" + std::to_string(i+1);
            pthread_create(&(threads_[i].tid), nullptr, HandlerTask, this);
        }
    }
 
    T Pop()
    {
        T t = tasks_.front();
        tasks_.pop();
        return t;
    }
 
    void Push(const T &t)
    {
        Lock();
        tasks_.push(t);
        Wakeup();
        Unlock();
    }
 
    static ThreadPool<T>* GetInstance()
    {
        // 如果后续线程想申请 Instance 实例会形成串行
        // 为提升效率我们可以在最外层套一层 if (nullptr == tp_)
        if (nullptr == tp_)
        {
            // 若获取线程池的单例本身就是多线程
            // 我们需要对获取线程池单例的操作加锁
            pthread_mutex_lock(&static_mutex_);
 
            // 懒汉模式
            // 需要使用时再初始化
            if (nullptr == tp_)
            {
                std::cout << "ThreadPool create success!" << std::endl;
                tp_ = new ThreadPool<T>();
            }
 
            pthread_mutex_unlock(&static_mutex_);
        }
 
        return tp_;
    }    
private:
    // 构造和析构函数私有化 —— 只允许调用  接口获取对象
    ThreadPool(int num = defalutnum)
        :threads_(num)
    {
        pthread_mutex_init(&mutex_, nullptr);
        pthread_cond_init(&cond_, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&mutex_);
        pthread_cond_destroy(&cond_);
    }
    // 禁用拷贝构造函数与赋值函数 防止出现 a=b=c 的情况
    ThreadPool(const ThreadPool<T>&) = delete;
    const ThreadPool<T>& operator=(const ThreadPool<T>&) = delete;
private:
    std::vector<ThreadInfo> threads_;
    std::queue<T> tasks_;
 
    pthread_mutex_t mutex_;
    pthread_cond_t cond_;
 
    // 只允许获取这一个指针
    static ThreadPool<T>* tp_;
    // 静态函数使用的锁
    static pthread_mutex_t static_mutex_;
};
 
// 初始化类内静态指针
template<class T>
ThreadPool<T>* ThreadPool<T>::tp_ = nullptr;
 
// 初始化类内静态锁
template<class T>
pthread_mutex_t ThreadPool<T>::static_mutex_ = PTHREAD_MUTEX_INITIALIZER;